package com.lan.iot;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import cn.bingoogolapple.qrcode.core.QRCodeView;

public class Add extends AppCompatActivity implements QRCodeView.Delegate{
    private QRCodeView qrCodeView;
    public static String DEV_COUNT="devcount";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_add);
        qrCodeView = (QRCodeView) findViewById(R.id.zxingview);
        qrCodeView.setDelegate(this);
    }
    String name=null;
    String devtag=null;
    int devtype=-1;
    int roomid=-1;
    View.OnClickListener clickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.kt:roomid=0;break;
                case R.id.zw:roomid=1;break;
                case R.id.cw:roomid=2;break;
            }
            addcount(name,devtag,devtype,roomid);
            Toast.makeText(Add.this, "添加"+name, Toast.LENGTH_LONG).show();
            finish();
        }
    };
    Dialog dlg = null;
    @Override
    public void onScanQRCodeSuccess(String result) {
        JSONObject JO= JSONObject.parseObject(result);
         name=JO.getString("name");
        devtag=JO.getString("tag");
//        Toast to = Toast.makeText(this, result+"8888888", Toast.LENGTH_SHORT);
//        to.setGravity(Gravity.CENTER, 0, 0);
//        to.show();
        devtype=JO.getInteger("type");
        final Drawable dr = getResources().getDrawable(R.drawable.radius_border);
        dlg = new AlertDialog.Builder(Add.this).create();
        dlg.show();//显示对话框
        Window window = dlg.getWindow();//获取对话框窗口\
        window.setGravity(Gravity.BOTTOM);//设置对话框显示剧中的位置
        window.setContentView(R.layout.select_room);//设置对话框布局
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        window.setBackgroundDrawable(dr);
        WindowManager.LayoutParams params = dlg.getWindow()
                .getAttributes();
        Button kt=window.findViewById(R.id.kt);
        kt.setOnClickListener(clickListener);
        Button zw=window.findViewById(R.id.zw);
        zw.setOnClickListener(clickListener);
        Button cw=window.findViewById(R.id.cw);
        cw.setOnClickListener(clickListener);
//                Display display = getWindowManager().getDefaultDisplay();
//                params.width = (int) (dpToPx(344));
//                        params.width=dpToPx(360);
//                params.dimAmount = 0.0f;
        dlg.getWindow().setAttributes(params);

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }
    @Override
    protected void onStart() {
        super.onStart();
        qrCodeView.startSpotAndShowRect();
    }

//    public void adddata(){
//        final SharedPreferences settings = getSharedPreferences(DEV_COUNT, Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor = settings.edit();
//        editor.commit();
//    }
public void addcount(String devname,String devtag,int devtype,int roomid){
    SharedPreferences settings = getSharedPreferences(DEV_COUNT, Context.MODE_PRIVATE);
    int devcount = settings.getInt("devcount", 0);
    ++devcount;
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("devcount", devcount);
    editor.putString("devname"+devcount, devname);
    editor.putString("devtag"+devcount, devtag);
    editor.putInt("devtype"+devcount, devtype);
    editor.putInt("roomid"+devcount, roomid);
    editor.commit();
}
    @Override
    protected void onStop() {
        qrCodeView.stopCamera();

if(name!=null&&roomid!=-1){
    Intent intent1 = new Intent("android.intent.action.CART_BROADCAST_adddev");
    intent1.putExtra("mdata",name );
    intent1.putExtra("devtype",devtype );
    LocalBroadcastManager.getInstance(Add.this).sendBroadcast(intent1);
    sendBroadcast(intent1);
        Intent intent2 = new Intent("android.intent.action.CART_BROADCAST_add");
        intent2.putExtra("mdata",name );
    intent2.putExtra("devtag",devtag );
    intent2.putExtra("devtype",devtype );
    intent2.putExtra("roomid",roomid );
    LocalBroadcastManager.getInstance(Add.this).sendBroadcast(intent2);
        sendBroadcast(intent2);}
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        qrCodeView.onDestroy();
        if(dlg != null) {
            dlg.dismiss();
        }
        super.onDestroy();
    }
}
